package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.api.publics.MapApi;
import com.jzo2o.api.publics.dto.response.LocationResDTO;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.CollUtils;
import com.jzo2o.common.utils.NumberUtils;
import com.jzo2o.customer.mapper.AddressBookMapper;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 地址薄 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-07-06
 */
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements IAddressBookService {
    @Resource
    private AddressBookMapper addressBookMapper;
    @Resource
    private MapApi mapApi;

    @Override
    public List<AddressBookResDTO> getByUserIdAndCity(Long userId, String city) {

        List<AddressBook> addressBooks = lambdaQuery()
                .eq(AddressBook::getUserId, userId)
                .eq(AddressBook::getCity, city)
                .list();
        if (CollUtils.isEmpty(addressBooks)) {
            return new ArrayList<>();
        }
        return BeanUtils.copyToList(addressBooks, AddressBookResDTO.class);
    }

    @Override
    public void add(AddressBookUpsertReqDTO addressBookUpsertReqDTO) {
        Long userId = UserContext.currentUserId();
        if (1 == addressBookUpsertReqDTO.getIsDefault()) {
            addressBookMapper.update(new AddressBook().setIsDefault(0), new LambdaQueryWrapper<AddressBook>().eq(AddressBook::getUserId, userId));
        }
        AddressBook addressBook = BeanUtil.toBean(addressBookUpsertReqDTO, AddressBook.class);
        addressBook.setUserId(userId);
        String location = addressBookUpsertReqDTO.getLocation();
        String[] split = location.split(",");
        addressBook.setLon(Double.parseDouble(split[0]));
        addressBook.setLat(Double.parseDouble(split[1]));
        addressBookMapper.insert(addressBook);

    }

    @Override
    public void update(Long id, AddressBookUpsertReqDTO addressBookUpsertReqDTO) {
        if (1 == addressBookUpsertReqDTO.getIsDefault()) {

            addressBookMapper.update(new AddressBook().setIsDefault(0), new LambdaQueryWrapper<AddressBook>().eq(AddressBook::getUserId, UserContext.currentUserId()));
        }
        AddressBook addressBook = BeanUtil.toBean(addressBookUpsertReqDTO, AddressBook.class);
        addressBook.setId(id);
        LocationResDTO locationByAddress = mapApi.getLocationByAddress(addressBookUpsertReqDTO.getAddress());
        String location = locationByAddress.getLocation();
        if (StrUtil.isNotEmpty(location)) {
            addressBook.setLon(NumberUtils.parseDouble(location.split(",")[0]));
            addressBook.setLat(NumberUtils.parseDouble(location.split(",")[1]));
        }
        addressBookMapper.updateById(addressBook);

    }

    @Override
    public void updateDefaultStatus(Long userId, Long id, Integer flag) {
        if (1 == flag){
            addressBookMapper.update(new AddressBook().setIsDefault(0), new LambdaQueryWrapper<AddressBook>().eq(AddressBook::getUserId, UserContext.currentUserId()));
        }
        AddressBook addressBook =new AddressBook();
        addressBook.setId(id);
        addressBook.setIsDefault(flag);
        addressBookMapper.updateById(addressBook);
    }

    @Override
    public AddressBookResDTO defaultAddress() {
    AddressBook addressBook = addressBookMapper.selectOne(new LambdaQueryWrapper<AddressBook>() .eq(AddressBook::getUserId, UserContext.currentUserId()).eq(AddressBook::getIsDefault, 1));
    return BeanUtil.toBean(addressBook, AddressBookResDTO.class);
    }

    @Override
    public PageResult<AddressBookResDTO> page(AddressBookPageQueryReqDTO addressBookPageQueryReqDTO) {
        Page<AddressBook> page = PageUtils.parsePageQuery(addressBookPageQueryReqDTO,AddressBook.class);
        Page<AddressBook> serveTypePage = addressBookMapper.selectPage(page,new LambdaQueryWrapper<AddressBook>().eq(AddressBook::getUserId, UserContext.currentUserId()));
        return PageUtils.toPage(serveTypePage, AddressBookResDTO.class);
    }
}
